package com.itheima.redbaby.activity;


import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.activity.LimitBuyActivity.MyLimitBuyAdpter;
import com.itheima.redbaby.activity.LimitBuyActivity.ViewHolder;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.engine.NewProductEngine;
import com.itheima.redbaby.engine.impl.NewProductEngineImpl;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.ImageUtil;
import com.itheima.redbaby.utils.PromptManager;

public class NewActivity   extends BaseActivity{

	private TextView textTitle;
	private ListView productList;
	private MyLimitBuyAdpter  adapter; 
	private  List<Product>   listData;  
	private NewProductEngine impl ;

	/* ==========在商品列表中需要的数据============= */
	private int productId; // 产品id
	private String productName; // 产品名字
	private float marketprice; // 产品市场价格
	private float price; // 产品会员价
	private String pic; // 产品图片地址

	/* ======================= */
	private  TextView  backTv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_activity);
		adapter  =  new MyLimitBuyAdpter();
		textTitle = (TextView) findViewById(R.id.textTitle);
		textTitle.setText("新品上架");
		productList = (ListView) findViewById(R.id.productList);
		impl  = DaoAndEngineFactory.getImpl(NewProductEngine.class);
		filldata();
		
		backTv = (TextView) findViewById(R.id.backTv);

			backTv.setOnClickListener( new  OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Intent intent_home = new Intent(NewActivity.this, HomeActivity.class);
						startActivity(intent_home);
						finish();
					}
				});
				
	}
	
	private void filldata() {
		new MyHttpTask() {
			protected void onPreExecute() {
				PromptManager.showProgressDialog(NewActivity.this);
			};
			/***
			 * 获取后台数据
			 */
			@Override
			protected Object doInBackground(Void... params) {

				List<Product> limitPList = impl.getNewPList(1, 15);
				System.out.println(limitPList.toString());
				return limitPList;

			}

			/**
			 * 获取后台返回 的数据 并且跟新ui 获得的是 topic 对象
			 * 
			 * 更新 holder.textContent.setText("酷 ~宝贝  ~宝贝~ 裤" + "时间");
			 * holder.imgIcon.setImageResource(imgsid[position]);
			 */
			@Override
			protected void onPostExecute(Object result) {
				listData = (List<Product>) result;
				
				productList.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				PromptManager.closeProgressDialog();
			}

		}.executeHttp();

		/**
		 * listview 中的跳转事件 点击时候我们 跳转到商品列表页面
		 */
		productList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				 Product product = listData.get(position);
				 int productId = product.getId();
				 Intent intent = new Intent(NewActivity.this,
						 ProductDetailActivity.class);
				 intent.putExtra("pId", productId);
				 startActivity(intent);
			}
		});
		
	}
	
	class  MyLimitBuyAdpter  extends  BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View view;
			ViewHolder holder;
			if (convertView != null) {
				view = convertView;
				holder = (ViewHolder) view.getTag();
			} else {
				view = View.inflate(NewActivity.this,
						R.layout.new_item, null);
				holder = new ViewHolder();
				holder.pro_icon = (ImageView) view.findViewById(R.id.pro_icon);
				
				
				holder.newName = (TextView) view.findViewById(R.id.newName);
				
				holder.new_marketPrice = (TextView) view.findViewById(R.id.new_marketPrice);
				holder.newprice = (TextView) view.findViewById(R.id.newprice);
				view.setTag(holder);
			}
			Product product = listData.get(position);
			productId = product.getId();
			productName = product.getName();
			marketprice = product.getMarketprice();
			price = product.getPrice();
			pic = product.getPic();

			ImageUtil.loadImageFromUrl(holder.pro_icon, pic);
			holder.newName.setText(productName);
			holder.new_marketPrice.setText("市场价"+marketprice);
			holder.newprice.setText("会员价"+price );
			
			return view;
		}
	}
	
	static  class  ViewHolder{
		ImageView  pro_icon;  //商品图片
		TextView  newName; //商品名字
		TextView  new_marketPrice;  //抢购之前的价格
		TextView newprice;//限时抢购的价格提示

		
	}
}
